package com.lal.sdk;

import com.lal.sdk.model.LalResponse;
import com.lal.sdk.proxy.StreamProxyManager;
import com.lal.sdk.util.HttpUtils;
import org.json.JSONObject;

import java.net.URL;

public class LalServerClient {
    private final String apiUrl;
    private final String streamUrl;
    private final StreamProxyManager proxyManager;
    public LalServerClient(String baseUrl,String streamUrl) {
        this(baseUrl, null, false,streamUrl);
    }

    public LalServerClient(String apiUrl, String proxyBaseUrl,String streamUrl) {
        this(apiUrl, proxyBaseUrl, true,streamUrl);
    }

    public LalServerClient(String apiUrl, String proxyBaseUrl, boolean enableSecurityProxy,String streamUrl) {
        this.apiUrl = apiUrl.replaceAll("/$", "");
        this.streamUrl = streamUrl;
        this.proxyManager = enableSecurityProxy && proxyBaseUrl != null ?
            new StreamProxyManager(proxyBaseUrl) : null;
    }

    public LalResponse startPullStream(String streamName, String rtspUrl) {
        String url = apiUrl + "/api/ctrl/start_relay_pull";
        JSONObject json = new JSONObject();
        json.put("stream_name", streamName);
        json.put("url", rtspUrl);
        return HttpUtils.post(url, json);
    }

    public LalResponse stopPullStream(String streamName) {
        String url = apiUrl + "/api/ctrl/stop_relay_pull"+"?stream_name="+streamName;
        return HttpUtils.get(url);
    }

    public LalResponse getStreamStatus(String streamName) {
        String url = apiUrl + "/api/stat/group?stream_name=" + streamName;
        return HttpUtils.get(url);
    }

    public String getFlvUrl(String streamName) {
        return streamUrl + "/live/" + streamName + ".flv";
    }

    public String getHlsUrl(String streamName) {
        return streamUrl + "/hls/" + streamName + "/playlist.m3u8";
    }

    public String getWebRtcUrl(String streamName) {
        return streamUrl + "/webrtc/play?stream_name=" + streamName;
    }

    // 新增安全代理方法
    public String getSecureFlvUrl(String streamName, String clientIp) {
        if (proxyManager == null) {
            throw new IllegalStateException("Security proxy not enabled. Initialize with proxyBaseUrl.");
        }
        return proxyManager.createSecureStreamUrl(streamName, clientIp, StreamProxyManager.StreamFormat.FLV);
    }

    public String getSecureHlsUrl(String streamName, String clientIp) {
        if (proxyManager == null) {
            throw new IllegalStateException("Security proxy not enabled. Initialize with proxyBaseUrl.");
        }
        return proxyManager.createSecureStreamUrl(streamName, clientIp, StreamProxyManager.StreamFormat.HLS);
    }

    public String getSecureWebRtcUrl(String streamName, String clientIp) {
        if (proxyManager == null) {
            throw new IllegalStateException("Security proxy not enabled. Initialize with proxyBaseUrl.");
        }
        return proxyManager.createSecureStreamUrl(streamName, clientIp, StreamProxyManager.StreamFormat.WEBRTC);
    }

    public boolean validateStreamAccess(String token, String streamName, String clientIp) {
        if (proxyManager == null) {
            throw new IllegalStateException("Security proxy not enabled. Initialize with proxyBaseUrl.");
        }
        return proxyManager.validateStreamAccess(token, streamName, clientIp);
    }

    public void revokeStreamAccess(String token) {
        if (proxyManager == null) {
            throw new IllegalStateException("Security proxy not enabled. Initialize with proxyBaseUrl.");
        }
        proxyManager.revokeStreamAccess(token);
    }

    public void cleanupExpiredSessions() {
        if (proxyManager != null) {
            proxyManager.cleanupExpiredSessions();
        }
    }
    public static String replaceUrlPort(String baseUrl, int newPort) {
        try {
            URL url = new URL(baseUrl);
            return String.format("%s://%s:%d%s",
                    url.getProtocol(),
                    url.getHost(),
                    newPort,
                    url.getPath()
            );
        } catch (Exception e) {
            throw new IllegalArgumentException("无效的URL: " + baseUrl, e);
        }
    }
}